package com.gable.report.controller;

import com.gable.report.common.util.MD5Util;
import com.gable.report.common.util.ReturnResultUtil;
import com.gable.report.model.User;
import com.gable.report.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统管理>用户管理
 * @author Aioria
 *
 */
@Controller
@RequestMapping("/user") /* 建议每个Controller都加上@RequestMapping采用Restful风格URL */
public class UserController {
	
	private final Log logger = LogFactory.getLog(this.getClass());

	@Resource
	HttpServletRequest request;
	
	@Resource
	HttpServletResponse response;
	
	@Autowired
	UserService userService;
	

	/**
	 * 功能说明：用户登录模块App
	 * 创建人：乔春峰
	 * 创建时间：2016-08-18
	 * @param username
	 * @param password
	 * @param token
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public ReturnResultUtil login(String username, String password, String token) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		Map<String,Object> resultMap = new HashMap<String, Object>();	// 接受返回值Map
		ReturnResultUtil returnResultUtil = new ReturnResultUtil(); 	//App返回对象
		resultMap = userService.login(username, MD5Util.md5(password),token);	//MD5Util.md5()给密码加密
		// 取出登陆状态
		String result = resultMap.get("result").toString();
		// 1表示登陆成功，如果登陆成功就向session放参数
		if("1".equals(result)){
			User user =  (User) resultMap.get("loginUser"); 			// 取出user对象
			if(user.getStatus() != null && user.getStatus() == 0){
				returnResultUtil.setRet(0);
				returnResultUtil.setMsg("该账号已经被禁用");
			}else{
				user.setToken(token);
				returnResultUtil.setRet(1);
				returnResultUtil.setMsg("登陆成功");
				Map<String,Object> date = new HashMap<String, Object>();
				date.put("userid", user.getId());									// id
				date.put("token", user.getToken());									// 用户唯一标识
				date.put("hospitalId", user.getHospitalId());						// 医院ID
				date.put("serviceProviderId", user.getServiceProviderId());			// 服务商ID
				date.put("departmentId", user.getDepartmentId());					// 科室ID
				date.put("department", user.getDepartment());						// 科室名称
				date.put("name", user.getName());									// 用户名字
				date.put("roleId", user.getRoleId());								// 角色ID
				date.put("role", user.getRole());									// 角色
				date.put("engineerHospitalId", user.getEngineerHospitalId());		// 驻场工程师							// 角色ID
				date.put("secretKey", user.getSecretKey());							// 授权码id
				date.put("hospital", user.getHospital());							// 医院名称
				date.put("serviceProvider", user.getServiceProvider());				// 维修商名称
				returnResultUtil.setData(date);
			}
		}else if("0".equals(result)){
			returnResultUtil.setRet(0);
			returnResultUtil.setMsg("账号或手机号码不存在");
		}else if("2".equals(result)){
			returnResultUtil.setRet(0);
			returnResultUtil.setMsg("密码错误");
		}else{
			returnResultUtil.setRet(0);
			returnResultUtil.setMsg("登陆异常");
		}
		return returnResultUtil;
	}
	

}
